package com.sugree.util.entity;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Message implements Comparable<Message>, Parcelable, Serializable {
	public final static String TAG = "Message";

	protected final static SimpleDateFormat[] FORMATTER = {
		new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US),
		new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US),
	};

	protected String mAuthor;
	protected String mTitle;
	protected URL mLink;
	protected List<String> mEnclosures = new ArrayList<String>();
	protected String mDescription;
	protected Date mPubDate;

	public Message() {
	}

	public Message(Message m) {
		mAuthor = m.mAuthor;
		mTitle = m.mTitle;
		mLink = m.mLink;
		mEnclosures = m.mEnclosures;
		mDescription = m.mDescription;
		mPubDate = m.mPubDate;
	}

	@Override
	public String toString() {
		return getLink();
	}

	@Override
	public int hashCode() {
		return mLink.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return mLink.toString().equals(((Message)obj).getLink());
	}

	public int compareTo(Message another) {
		if (another == null) {
			return 1;
		}
		return another.mPubDate.compareTo(mPubDate);
	}

	public String getAuthor() {
		return mAuthor;
	}

	public void setAuthor(String author) {
		mAuthor = author;
	}

	public String getTitle() {
		return mTitle;
	}

	public void setTitle(String title) {
		mTitle = title;
	}

	public String getLink() {
		return mLink.toString();
	}

	public void setLink(String link) {
		try {
			mLink = new URL(link);
		} catch (MalformedURLException e) {
			Log.e(TAG, e.getMessage()+": "+link);
			throw new RuntimeException(e);
		}
	}

	public List<String> getEnclosures() {
		if (mEnclosures == null) {
			return null;
		}
		return mEnclosures;
	}

	public void addEnclosure(String link) {
		mEnclosures.add(link);
	}

	public String getDescription() {
		return mDescription;
	}

	public void setDescription(String description) {
		mDescription = description;
	}

	public String getPubDate() {
		return FORMATTER[0].format(mPubDate);
	}

	public void setPubDate(String pubDate) {
		pubDate = pubDate.replace("GMT", "+0000");
		for (SimpleDateFormat format : FORMATTER) {
			try {
				mPubDate = format.parse(pubDate.trim());
				break;
			} catch (ParseException e) {
			}
		}
		if (mPubDate == null) {
			mPubDate = new Date();
		}
	}

	public int describeContents() {
		return 0;
	}

	public static final Parcelable.Creator<Message> CREATOR = new Parcelable.Creator<Message>() {
		public Message createFromParcel(Parcel in) {
			return new Message(in);
		}

		public Message[] newArray(int size) {
			return new Message[size];
		}
	};

	private Message(Parcel in) {
		readFromParcel(in);
	}

	public void writeToParcel(Parcel out, int flags) {
		out.writeString(mAuthor);
		out.writeString(mTitle);
		out.writeSerializable(mLink);
		out.writeStringList(mEnclosures);
		out.writeString(mDescription);
		out.writeSerializable(mPubDate);
	}

	public void readFromParcel(Parcel in) {
		mAuthor = in.readString();
		mTitle = in.readString();
		mLink = (URL)in.readSerializable();
		mEnclosures.clear();
		in.readStringList(mEnclosures);
		mDescription = in.readString();
		mPubDate = (Date)in.readSerializable();
	}
}
